//
//  QianBaoViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/9.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "QianBaoViewController.h"
#import "CommonData.h"
#import <Masonry.h>
#import "QianBaoTopTableViewCell.h"
#import "QianBaoSegmTableViewCell.h"
#import "QianBaoHeaderTableViewCell.h"
#import "QianBaoChongViewController.h"
#import "QianBaoTiViewController.h"

#import "taskOverviewService.h"

@interface QianBaoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)    UITableView *tableView;

///记录表
@property (nonatomic,strong)    NSArray<dataOverviewDetailRecordList*>* recordList;
@property (nonatomic,strong)    dataOverviewDetailInfo  *modelDetail;
@end

@implementation QianBaoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"GTE";
    
    [self tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self setBlackStatus:NO];
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBar.hidden = YES;
    
    [self loadData:@"all"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - loadData

-(void)loadData:(NSString *)strSourceType{
    if (self.isGTE) {
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:@"0" forKey:@"page"];
        [dict setValue:@"50" forKey:@"pageSize"];
        ///all :全部    0：充值    1：提现
        [dict setValue:strSourceType forKey:@"sourceType"];
        
        [DKProgressHUD showLoading];
        [taskOverviewService getGTEOverviewDetail:dict success:^(dataOverviewDetail *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                self.modelDetail = response.data;
                self.recordList = response.data.recordList;
                [self.tableView reloadData];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD dismiss];
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }else{
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:@"0" forKey:@"page"];
        [dict setValue:@"50" forKey:@"pageSize"];
        ///all :全部    0：充值    1：提现
        [dict setValue:strSourceType forKey:@"sourceType"];
        
        [DKProgressHUD showLoading];
        [taskOverviewService getGTKOverviewDetail:dict success:^(dataOverviewDetail *response, BOOL success) {
            [DKProgressHUD dismiss];
            if (success) {
                self.modelDetail = response.data;
                self.recordList = response.data.recordList;
                [self.tableView reloadData];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            [DKProgressHUD dismiss];
            [DKProgressHUD showErrorWithStatus:strDescription];
        }];
    }
}



#pragma mark - tableivew


-(UITableView *)tableView{
    if (!_tableView) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight+20 ) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        //定义显示默认cell的横线
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
    }
    return _tableView;
}

#pragma mark - UITableViewDelegate

//定义每一个section有多少row
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }
    
    return [self.recordList count];
}

//定义有多少section
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

//定义CellRow的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return 318;
    }
    return 45;
}

//定义section的Footer的view视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的Footer的高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}


//定义section的header的view视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [UIView new];
    view.backgroundColor = COLOR_Background;
    return view;
}

//定义section的header的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

//主体cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            QianBaoTopTableViewCell *cell = [QianBaoTopTableViewCell cellWithTableView:tableView IndexPath:indexPath];
            [cell.btnBack addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
            [cell.btnChong addTarget:self action:@selector(goChong:) forControlEvents:UIControlEventTouchUpInside];
            [cell.btnTi addTarget:self action:@selector(goTi:) forControlEvents:UIControlEventTouchUpInside];
            [cell.btnRight addTarget:self action:@selector(rightClick:) forControlEvents:UIControlEventTouchUpInside];
            
            NSString *typeName = self.isGTE ? @"GTE" :@"GTK";
            cell.labelTitle.text = typeName;
            cell.labelCNYAmount.text = [NSString stringWithFormat:@"≈%@CNY",self.modelDetail.cnyAmount];
            cell.labelTotalAmount.text =  [NSString stringWithFormat:@"%@ %@",self.modelDetail.totalAmount,typeName];
            cell.labelFreezeAmount.text = [NSString stringWithFormat:@"冻结资产：%@ %@",self.modelDetail.freezeAmount,typeName];
            cell.labelAvailableAmount.text = [NSString stringWithFormat:@"可用资产：%@ %@",self.modelDetail.availableAmount,typeName];
            
            ///右上角切换钱包
            [cell.btnRight setTitle:self.isGTE ? @"GTK" :@"GTE" forState:UIControlStateNormal];
            
            return cell;
        }else if (indexPath.row == 1) {
            ///全部--充值---提现列表
            QianBaoSegmTableViewCell *cell = [QianBaoSegmTableViewCell cellWithTableView:tableView IndexPath:indexPath];
             [cell.segmCheck addTarget:self action:@selector(segmentValueChanged:)forControlEvents:UIControlEventValueChanged];
            return cell;
        }else if (indexPath.row == 2) {
            QianBaoHeaderTableViewCell *cell = [QianBaoHeaderTableViewCell cellWithTableView:tableView IndexPath:indexPath];
            return cell;
        }
    }else{
        QianBaoHeaderTableViewCell *cell = [QianBaoHeaderTableViewCell cellWithTableViewList:tableView IndexPath:indexPath];
        CGFloat diffS = indexPath.row%2;
        if (diffS==0) {
            cell.backgroundColor = [UIColor whiteColor];
        }else{
            cell.backgroundColor = LCFromRGB(251, 251, 251);
        }
        
        dataOverviewDetailRecordList *mm = [self.recordList objectAtIndex:indexPath.row];
        dataOverviewDetailRecordList *mmm = [dataOverviewDetailRecordList mj_objectWithKeyValues:mm];
        cell.labelLeft.text =  mmm.amount; //@"-80.00000000";
        cell.labelCenter.text = mmm.status;// @"已完成";
        cell.labelRight.text = mmm.operateDate;// @"06/13 16:46";
        return cell;
    }
    
    return [UITableViewCell  new];
}

///切换列表类型
-(void)segmentValueChanged:(UISegmentedControl*)sender{
    switch (sender.selectedSegmentIndex) {
        case 0:
        {///全部
            [self loadData:@"all"];
        }
            break;
        case 1:
        {///充值
            [self loadData:@"0"];
        }
            break;
        case 2:
        {///提现
            [self loadData:@"1"];
        }
            break;
        default:
            break;
    }
}


///切换钱包
-(void)rightClick:(UIButton*)sender{
    self.isGTE = !self.isGTE;
    [self loadData:@"all"];
}

///充币
-(void)goChong:(UIButton*)sender{
    
    QianBaoChongViewController *vc = [[QianBaoChongViewController alloc]initWithNibName:@"QianBaoChongViewController" bundle:nil];
    vc.isGTE = self.isGTE;
    [self.navigationController pushViewController:vc animated:YES];
}

///提币
-(void)goTi:(UIButton*)sender{
    
    QianBaoTiViewController *vc = [[QianBaoTiViewController alloc]initWithNibName:@"QianBaoTiViewController" bundle:nil];
    vc.isGTE = self.isGTE;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
